/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/6/17 22:02
 */
// 删除排序链表中的重复元素II
//给定一个已排序的链表的头 head ， 删除原始链表中所有重复数字的节点，只留下不同的数字 。返回 已排序的链表 。
public class Solution82 {
    public ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) return head;
        ListNode dummy = new ListNode(-1);
        dummy.next = head;
        ListNode cur = head;
        ListNode pre = dummy;

        while (cur != null) {
            ListNode next = cur.next;
            int steps = 0;
            while (next != null && cur.val == next.val) {
                next = next.next;
                steps++;
            }
            if (steps > 0) {
                pre.next = next;
            } else {
                pre = cur;
            }
            cur = next;
        }
        return dummy.next;
    }
}
